/**
 * 
 */
package com.nxn.core.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@NamedQueries({
	@NamedQuery(
		name = "findUserByUsernameAndPassword",
		query = "from User u where u.username = :username and u.password = :password"
	)
})
@Entity
@Table(name = "t_user")
public class User extends IdEntity {

	private String username;
	private String password;
	private String name;
	private String email;
	private String phone;
	private boolean locked;
	private Set<Role> roles = new HashSet<Role>();
	
	@Column(name="username", nullable=false, length=50, insertable=true)
	public String getUsername() {
		return username;
	}
	@Column(name = "password")
	public String getPassword() {
		return password;
	}
	@Column(name = "name")
	public String getName() {
		return name;
	}
	@Column(name = "email")
	public String getEmail() {
		return email;
	}
	@Column(name = "phone")
	public String getPhone() {
		return phone;
	}
	@Column(name = "locked")
	public boolean getLocked() {
		return locked;
	}
	//多对多定义
	@ManyToMany
	@JoinTable(name = "t_user_role", 
			joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id ASC")
	public Set<Role> getRoles() {
		return roles;
	}
	
	
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
}